package com.supreme.uploadserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.supreme.commonboot.exception.BizException;
import com.supreme.commonboot.utils.AjaxResp;
import com.supreme.uploadserver.component.MinIoService;
import com.supreme.uploadserver.service.FileProcService;
import io.minio.messages.Bucket;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//@RestController
//@RequestMapping(value = "minio")
public class MinioControllerOld {

    @Value("${upload-server.uploadPath}")
    private String supremeUploadPath;

    @Resource
    private FileProcService fileProcService;

    @Resource
    private MinIoService minIoService;

    @GetMapping(value = "getBucketList")
    public AjaxResp getBucketList() {
        final List<Bucket> bucketList = minIoService.getBucketList();
        final List<String> stringList = bucketList.stream().map(Bucket::name).collect(Collectors.toList());
        return AjaxResp.instance(stringList);
    }

    @PostMapping(value = "createBucket")
    public AjaxResp createBucket(@RequestBody JSONObject bodyData) {
        final String bucketName = bodyData.getString("bucketName");
        minIoService.createBucket(bucketName);
        return AjaxResp.instance();
    }

    @PostMapping(value = "pathToBucket")
    public AjaxResp pathToBucket(
            @RequestParam(value = "file") MultipartFile multipartFile,
            @RequestParam(value = "bucketName") String bucketName) throws IOException {
        if (multipartFile.isEmpty()) {
            throw new BizException("multipart file is empty");
        }

        String contentType = multipartFile.getContentType();
        long size = multipartFile.getSize();
        String originalFilename = multipartFile.getOriginalFilename();
        String fieldName = multipartFile.getName();

        Path newFilePath = fileProcService.storeUploadFile(supremeUploadPath, originalFilename, multipartFile::transferTo);
        minIoService.upload(bucketName, originalFilename, newFilePath.toString(), contentType);

        return new AjaxResp()
                .append("contentType", contentType)
                .append("size", size)
                .append("fieldName", fieldName)
                .append("originalFilename", originalFilename);
    }

    @PostMapping(value = "streamToBucket")
    public AjaxResp streamToMinIO(
            @RequestParam(value = "file") MultipartFile multipartFile,
            @RequestParam(value = "bucketName") String bucketName) throws IOException {
        if (multipartFile.isEmpty()) {
            throw new BizException("multipart file is empty");
        }

        String contentType = multipartFile.getContentType();
        long size = multipartFile.getSize();
        String originalFilename = multipartFile.getOriginalFilename();
        String fieldName = multipartFile.getName();

        final InputStream inputStream = multipartFile.getInputStream();
        minIoService.upload(bucketName, originalFilename, inputStream, size, contentType);

        return new AjaxResp()
                .append("contentType", contentType)
                .append("size", size)
                .append("fieldName", fieldName)
                .append("originalFilename", originalFilename);
    }

    @GetMapping(value = "getObjectUrl")
    public AjaxResp getObjectUrl(@RequestParam Map<String, String> queryParams) {
        final String bucketName = queryParams.get("bucketName");
        final String objectName = queryParams.get("objectName");
        final String expire = queryParams.get("expire");
        final int expireSecond = Integer.parseInt(expire);
        final String fileUrl = minIoService.getFileUrl(bucketName, objectName, expireSecond);
        return AjaxResp.instance(fileUrl);
    }
}
